package com.ylbh.modules.member.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ylbh.common.pojo.freight.vo.FreightJson;
import com.ylbh.common.pojo.member.dto.AreaDTO;
import com.ylbh.common.pojo.member.vo.AreaVo;
import com.ylbh.common.utils.FastJsonUtil;
import com.ylbh.common.utils.StringUtils;
import com.ylbh.modules.freight.entity.ShoppingFreight;
import com.ylbh.modules.freight.mapper.ShoppingFreightMapper;
import com.ylbh.modules.member.mapper.AreaMapper;
import com.ylbh.modules.member.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 * @author mgh
 */
@Service("areaService")
public class AreaServiceImpl extends ServiceImpl<AreaMapper, AreaDTO> implements AreaService {
    @Autowired
    private AreaMapper areaMapper;

    @Autowired
    private ShoppingFreightMapper shoppingFreightMapper;


    /**
     * 获取地区数据列表
     * @param id
     * @return
     */
    @Override
    public List<AreaVo> queryByLevel(int id) {
        return areaMapper.selectByLevel(id);
    }

    /**
     * 返回所有省信息
     * @return
     */
    @Override
    public List<AreaVo> queryProvinceList() {
        return areaMapper.selectProvinceList();
    }

    /**
     * 返回下级的名字及id
     * @param id
     * @return
     */
    @Override
    public List<AreaVo> querySubordinateList(Long id) {
        return areaMapper.selectSubordinateList(id);
    }

    /**
     * 根据地区id获取信息
     * @param id
     * @return
     */
    @Override
    public List<AreaVo> getAddressDetailsById(Integer id) {
        // 根据运费id获取运费模板
        ShoppingFreight shoppingFreight = shoppingFreightMapper.selectById(id);
        List<FreightJson> list = FastJsonUtil.toList(shoppingFreight.getSpecialAreaJson(), FreightJson.class);
        List<AreaVo> areaVos = new ArrayList<>();
        for (FreightJson f:list) {
            if (!StringUtils.isEmpty(f.getCityId())) {
                AreaVo vo = areaMapper.selectAddressDetailsById(f.getCityId());
                areaVos.add(vo);
            }
        }
        return areaVos;
    }
}
